package org.catspaw.cherubim.security.rbac.web;

import java.util.List;
import java.util.UUID;

import javax.faces.event.ActionEvent;

import org.catspaw.cherubim.pagination.DefaultPageData;
import org.catspaw.cherubim.pagination.PageData;
import org.catspaw.cherubim.pagination.jsf.PaginationListDataModel;
import org.catspaw.cherubim.security.rbac.dao.ResourceDao;
import org.catspaw.cherubim.security.rbac.model.ResourceModel;

public class ResourceManagedBean {

	private PaginationListDataModel<ResourceModel>	dataModel;
	private String									id;
	private String									code;
	private String									resourceString;
	private ResourceDao								resourceDao;

	public ResourceManagedBean() {
		dataModel = new PaginationListDataModel<ResourceModel>(
				PageData.DEFAULT_PAGE_SIZE) {

			@Override
			public PageData<ResourceModel> fetchPage(int startRow, int pageSize) {
				int totalCount = resourceDao.total().intValue();
				List<ResourceModel> data = resourceDao.find(startRow, pageSize);
				return new DefaultPageData<ResourceModel>(data, totalCount,
						startRow, pageSize);
			}
		};
	}

	public void openEdit(ActionEvent event) {
		id = (String) event.getComponent().getAttributes().get("id");
		code = (String) event.getComponent().getAttributes().get("code");
		resourceString = (String) event.getComponent().getAttributes()
				.get("resourceString");
	}

	public void edit() {
		ResourceModel resource = resourceDao.get(id);
		resource.setCode(code);
		resource.setResourceString(resourceString);
		resourceDao.update(resource);
		dataModel.refresh();
	}

	public void remove(ActionEvent event) {
		id = (String) event.getComponent().getAttributes().get("id");
		ResourceModel model = resourceDao.get(id);
		resourceDao.delete(model);
		dataModel.refresh();
	}

	public void openAdd(ActionEvent event) {
		id = "";
		code = "";
		resourceString = "";
	}

	public void add() {
		ResourceModel model = resourceDao.create();
		model.setId(UUID.randomUUID().toString());
		model.setCode(code);
		model.setResourceString(resourceString);
		resourceDao.save(model);
		dataModel.setVisitable(true);
		dataModel.refresh();
	}

	public void search(ActionEvent event) {
		dataModel.setVisitable(true);
		dataModel.setCurrentPage(1);
		dataModel.refresh();
	}

	public PaginationListDataModel<ResourceModel> getDataModel() {
		return dataModel;
	}

	public String getId() {
		return id;
	}

	public String getCode() {
		return code;
	}

	public String getResourceString() {
		return resourceString;
	}

	public ResourceDao getResourceDao() {
		return resourceDao;
	}

	public void setDataModel(PaginationListDataModel<ResourceModel> dataModel) {
		this.dataModel = dataModel;
	}

	public void setId(String id) {
		this.id = id;
	}

	public void setCode(String code) {
		this.code = code;
	}

	public void setResourceString(String resourceString) {
		this.resourceString = resourceString;
	}

	public void setResourceDao(ResourceDao resourceDao) {
		this.resourceDao = resourceDao;
	}
}
